package com.cuit.limit;

import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.concurrent.atomic.AtomicLong;

@Slf4j
public class FlowController {
    private AtomicLong[] metrics = new AtomicLong[]{new AtomicLong(0), new AtomicLong(0), new AtomicLong(0)};


    public void incrementAtCurrentMinute(){
        Date date = new Date();
        int index = (int) ((date.getTime() / 60000) % 3);

        AtomicLong metric = metrics[index];
        metric.incrementAndGet();
    }

    public long getLastCallCountAtLastMinute(){
        Date date = new Date();
        int index = (int) ((date.getTime() / 60000 - 1 ) % 3);
        AtomicLong metric = metrics[index];
        return metric.get();
    }

    public long getCurrentCallCount(){
        Date date = new Date();
        int index = (int) ((date.getTime() / 60000 ) % 3);
        AtomicLong metric = metrics[index];
        return metric.get();
    }

    public long getNextCallCount(){
        Date date = new Date();
        int index = (int) ((date.getTime() / 60000 + 1 ) % 3);
        AtomicLong metric = metrics[index];
        return metric.get();
    }

    public void clearNextMinuteCallCount(){
        log.info("清理下一个时间矩阵");
        Date date = new Date();
        int index = (int) ((date.getTime() / 60000 + 1 ) % 3);
        AtomicLong metric = metrics[index];
        metric.set(0);
    }

    public AtomicLong[] getMetrics(){
        return metrics;
    }

    public void setMetrics(AtomicLong[] metrics){
        this.metrics = metrics;
    }

}
